//index.js
//获取应用实例
const app = getApp();
var getlist = function(that) {
    // var drivers = app.getApi.drivers;
    wx.showLoading({
        title: "加载中"
    });
    app.getApi.request(
        that.data.url, {},
        "POST",
        res => {
            wx.hideLoading();
            if (res.data.responseCode == 1000) {
                if (res.data.obj.length > 0) {
                    that.setData({
                        items: items.concat
                    });
                }
                if (that.data.pageIndex > 1 && res.data.obj.length == 0) {
                    that.setData({
                        isAll: true
                    });
                }
                if (that.data.pageIndex == 1 && res.data.obj.length == 0) {
                    that.setData({
                        noData: true
                    });
                }
            }
            // if(res.data.)
        },
        res => {}
    );
};

Page({
    data: {
        motto: "Hello World",
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse("button.open-type.getUserInfo"),
        nav: [
            { name: "银行存款", money: "1705855.7" },
            { name: "刷卡", money: "560202.41" },
            { name: "现金", money: "1705855.7" }
        ],
        navIndex: 0,
        role: wx.getStorageSync("Role"),
        infor: {
            total: 7855585
        },
        pageIndex: 1,
        items: [],
        isAll: false,
        noData: false,
        hidden: false,
        Pullcount: 0,
        priceList: {}
    },
    //事件处理函数
    deposit: function() {
        wx.navigateTo({
            url: "../deposit/deposit"
        });
    },
    record: function() {
        wx.navigateTo({
            url: "../record/record"
        });
    },
    remake: function() {
        wx.navigateTo({
            url: "../remake/remake"
        });
    },
    onShow: function() {
        if (app.globalData.userInfo) { 
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            });
            this.getUserOpenid();
        } else if (this.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                });

                this.getUserOpenid();
            };
        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo;
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    });
                    this.getUserOpenid();
                }
            });
        }
    },
    getUserOpenid() {
        //获取用户信息
        var that = this,
            nickName = app.globalData.userInfo.nickName; //用户昵称
        if (!app.globalData.openId) {

            app.api.request(
                "/api/LoginIn/Post", { Code: app.globalData.code, NickName: nickName },
                "POST",
                data => {
                    var datas = JSON.parse(data.data);

                    app.globalData.openId = datas.OpenId,
                        app.globalData.role = datas.Role;
                    wx.setStorageSync("Role", datas.Role);
                    app.api.requestFrom(
                        "/WXAccount/WxaccountInfo", {
                            nickName: nickName,
                            openId: datas.OpenId
                        }, "POST", data => {

                        }
                    )
                    that.setData({
                        role: datas.Role
                    });
                    // if (datas.Role == '店长') {
                    app.api.request(
                        "/api/saleamountinfo/" + datas.OpenId, {},
                        "GET",
                        res => {
                            var priceList = JSON.parse(res.data);
                            that.setData({
                                priceList: priceList
                            })
                        },
                        res => {}
                    );
                    // }

                }
            );
        }else{
          app.api.request(
            "/api/saleamountinfo/" + app.globalData.openId, {},
            "GET",
            res => {
              var priceList = JSON.parse(res.data);
              that.setData({
                priceList: priceList
              })
            },
            res => { }
          );
        }
    },
    getUserInfo: function(e) {
        app.globalData.userInfo = e.detail.userInfo;
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        });
    },
    nav(e) {
        var index = e.currentTarget.dataset.index;
        if (index == 0) {
            this.setData({
                items: [{
                        item: "10:41:23",
                        bankName: "建设银行",
                        moeny: 3232,
                        path: "../../images/jianshe.png"
                    },
                    {
                        item: "00:02:36",
                        bankName: "工商银行",
                        moeny: 623,
                        path: "../../images/gongshang.png"
                    },
                    {
                        item: "17:20:10",
                        bankName: "交通银行",
                        moeny: 1212,
                        path: "../../images/jiaotong.png"
                    },
                    {
                        item: "10:40:10",
                        bankName: "民生银行",
                        moeny: 45455,
                        path: "../../images/minsheng.png"
                    }
                ]
            });
        } else if (index == 1) {
            this.setData({
                items: []
            });
        } else {
            this.setData({
                items: [{
                        item: "10:41:23",
                        bankName: "存款",
                        moeny: 3232,
                        path: "../../images/jianshe.png"
                    },
                    {
                        item: "00:02:36",
                        bankName: "存款",
                        moeny: 623,
                        path: "../../images/gongshang.png"
                    },
                    {
                        item: "17:20:10",
                        bankName: "存款",
                        moeny: 1212,
                        path: "../../images/jiaotong.png"
                    },
                    {
                        item: "10:40:10",
                        bankName: "存款",
                        moeny: 45455,
                        path: "../../images/minsheng.png"
                    }
                ]
            });
        }
        this.setData({
            navIndex: index
        });
    },
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh();
    },
    onReachBottom() {
        var that = this;
        if (!that.data.isAll && that.data.Pullcount == 0) {
            that.setData({
                pageIndex: that.data.pageIndex + 1
            });
            that.page = that.page + 1;
            that.loadMore = true;
            // getlist(that)
        } else {
            that.hidden = true;
        }
    }
});